package com.test.mchelepesa.dialog_rice;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.test.mchelepesa.R;
import com.test.mchelepesa.bean_rice.user_info_rice.BankAccountList_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.util_rice.http_rice.HttpManager_rice;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class SubmitAccountDialog_rice extends Dialog {

    @SuppressLint("SetTextI18n")
    public SubmitAccountDialog_rice(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_submit_account_rice);

        getWindow().setBackgroundDrawableResource(R.color.trans);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setCanceledOnTouchOutside(false);

        LinearLayout groupLayout = findViewById(R.id.submit_account_group);

        ArrayList<BankAccountList_rice.Account> accountList = BankAccountList_rice.getInstance().getBankAccountList();

        for (BankAccountList_rice.Account account:accountList){
            View item = View.inflate(getContext(),R.layout.item_account_show,null);

            ImageView logoIv = item.findViewById(R.id.show_account_iv_logo);
            TextView channelTv = item.findViewById(R.id.show_account_tv_channel);
            TextView mobileTv = item.findViewById(R.id.show_account_tv_mobile);

            String logoUrl = account.getWalletLogo();
            if (!TextUtils.isEmpty(logoUrl)){
                new Thread(() -> {
                    try {
                        URL url = new URL(logoUrl);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(4995);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            new Handler(Looper.getMainLooper()).post(() -> logoIv.setImageBitmap(bitmap));
                        }
                    }catch (Exception e){
                        StringUtil_rice.printException(e);
                    }
                }).start();
            }

            channelTv.setText(account.getWalletDesc());
            mobileTv.setText(account.getWalletMobile());

            groupLayout.addView(item);

        }

        findViewById(R.id.submit_account_close).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_SUBMIT_ACCOUNT_CLOSE_CLICK");
            dismiss();
        });

        findViewById(R.id.submit_account_submit).setOnClickListener(v -> {
            HttpManager_rice.getInstance().saveUserBuriedPoint("DIALOG_SUBMIT_ACCOUNT_SUBMIT_CLICK");
            dismiss();
            HttpManager_rice.getInstance().requestUserProcess();
        });

    }


}
